import {Component, OnInit} from '@angular/core';
import {ActivityService} from "../service/activity.service";
import {Activity} from "../entity/activity";
import {ActivatedRoute} from "@angular/router";
import {User} from "../entity/user";

@Component({
  moduleId: module.id,
  selector: 'my-activity',
  templateUrl: 'activity.component.html',
  styleUrls: ['activity.component.css'],
})

export class ActivityComponent implements OnInit{

  constructor(private activityService:ActivityService, private route: ActivatedRoute){}

  activity:Activity;
  users:User[];
  currentRate:number;
  readonly = false;

  ngOnInit() {
    const id = this.route.snapshot.params['id'];
    this.activityService.getActivity(id).subscribe((activity) => {
      this.activity = activity;
      this.currentRate = activity.likeNumber;
      this.campaignUsers();
    } )
  }

  rateChange() {
    this.readonly = true;
    this.activity.likeNumber = parseInt(((this.activity.likeNumber + this.currentRate) / 2).toString());
    this.activityService.update(this.activity);
  }

  campaign() {
    this.activityService.campaign(this.activity.id).subscribe(() => {
      this.campaignUsers();
    } );
  }

  campaignUsers() {
    this.activityService.getUsersByActivityId(this.activity.id).subscribe((users) => this.users = users);
  }

}
